<template>
	<view class="container">
		<view class="search-box">
			<view class="search-before-box" @tap="showSearchAfter" v-show="isShowSearchBefore">
				<view class="search-before-input">
					<view class="search-img">
						<image src="../../static/images/search.png"></image>
					</view>
					<view class="search-txt">搜索大学</view>
				</view>
			</view>
			<view class="search-after-box" v-show="isShowSearchAfter">
				<view class="search-after-input">
					<view class="search-img">
						<image src="../../static/images/search.png"></image>
					</view>
					<input :focus="isSearchFocus" class="search-input" placeholder="搜索大学" placeholder-class="input-placeholder" v-model="reqData.keywords" @confirm="searchData" />
				</view>
				<view class="search-cancel" @tap="showSearchBefore">取消</view>
			</view>
		</view>

		<view class="list-box">
			<view class="item-line"></view>
			<view class="list-item" v-for="(item,index) in dataList" :key="item.universityId" @tap="goDetail(item.universityId)">
				<view class="list-item-left">
					<image mode="aspectFit" :src="item.universityLogoPic"></image>
				</view>
				<view class="list-item-middle">
					<view class="university-name">{{item.universityName}}</view>
					<view class="university-label-box">
						<view class="university-label" v-for="(itemLabel,labeIndex) in item.universityLabelList" :key="itemLabel.labelId">{{itemLabel.labelName}}</view>
					</view>
				</view>
				<view class="list-item-more">
					<image src="../../static/images/more.png"></image>
				</view>
			</view>
			<view class="loading" v-show="isLoading">{{loadingText}}</view>
		</view>
		
		<view class="no-trends-box" v-if="isEmpty">
			<view class="no-trends-img">
				<image src="../../static/images/no_data.png"></image>
			</view>
			<view class="no-trends-label">暂无数据</view>
		</view>
		
	</view>
</template>

<script>
	import { http, showToastMsg } from '../../utils'

	export default {
		data() {
			return {
				isShowSearchBefore: true,
				isShowSearchAfter: false,
				isSearchFocus: false,
				reqData: {
					keywords: '',
					curPage: 1
				},
				dataList: [],
				isLoading: false,
				loadingText: '正在加载...',
				isEmpty: false
			}
		},
		
		onLoad() {
			this.fetchData()
		},
		
		onReachBottom() {
			if (this.isLoading) {
				return false
			}
			this.isLoading = true
			this.fetchData()
		},
		
		methods: {
			
			searchData() {
				this.reqData.curPage = 1
				this.fetchData()
			},
			
			fetchData() {
				const that = this
				if (that.reqData.curPage == 1) {
					that.dataList = []
					let cacheData = http('/university/list', that.reqData, {isCache: true})
					if (cacheData && cacheData.data && cacheData.data.list) {
						that.dataList = that.dataList.concat(cacheData.data.list)
					}
					
					setTimeout(function() {
						http('/university/list', that.reqData, {isSetCache: true}).then(res => {
							if (res.code == 20000) {
								let data = res.data
								if (data.length > 0) {
									that.isLoading = false
									that.dataList = []
									that.reqData.curPage++
									that.dataList = that.dataList.concat(data)
								} else {
									that.isEmpty = true
								}
							} else {
								that.dataList = []
								that.isEmpty = true
							}
						})
					}, 1000)
				} else {
					http('/university/list', that.reqData).then(res => {
						if (res.code == 20000) {
							let data = res.data
							if (data.length > 0) {
								that.isLoading = false
								that.reqData.curPage++
								that.dataList = that.dataList.concat(data)
							} else {
								that.loadingText = '就这么多了'
							}
						}
					})
				}
			},
			
			showSearchAfter() {
				this.isShowSearchBefore = false
				this.isShowSearchAfter = true
				this.isSearchFocus = true
			},
			
			showSearchBefore() {
				this.isShowSearchBefore = true
				this.isShowSearchAfter = false
				this.isSearchFocus = false
			},
			
			goDetail(id) {
				uni.navigateTo({
					url: '/pages/admission_data/admission_data?id=' + id
				})
			}
		}
	}
</script>

<style>
	.loading {
		width: 100%;
		color: #666666;
		font-size: 30rpx;
		text-align: center;
		margin-top: 40rpx;
	}
	
	.no-trends-box {
		width: 750rpx;
		height: auto;
		margin-top: 300rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 200rpx;
	}
	
	.no-trends-img image {
		width: 300rpx;
		height: 300rpx;
	}
	
	.no-trends-label {
		font-size: 36rpx;
		color: #666666;
		margin-top: 10rpx;
	}

	.container {
		width: 750rpx;
		height: auto;
	}

	.search-box {
		width: 750rpx;
		height: 88rpx;
		background-color: #ffffff;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.search-before-input {
		width: 702rpx;
		height: 64rpx;
		border: solid 2rpx #d7d6d6;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		border-radius: 32rpx;
	}

	.search-after-box {
		width: 100%;
		height: 88rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.search-after-input {
		width: 600rpx;
		height: 64rpx;
		border: solid 2rpx #d7d6d6;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		border-radius: 32rpx;
		padding-left: 20rpx;
	}

	.input-placeholder {
		font-size: 24rpx;
		color: #999999;
	}

	.search-input {
		margin-left: 20rpx;
		height: 64rpx;
	}

	.search-cancel {
		font-size: 24rpx;
		color: #999999;
		margin-left: 10rpx;
	}

	.search-txt {
		width: 120rpx;
		font-size: 24rpx;
		color: #999999;
		text-align: center;
	}

	.search-img image {
		width: 33rpx;
		height: 33rpx;
		margin-top: 8rpx;
	}

	.list-box {
		width: 750rpx;
		height: auto;
		display: flex;
		flex-direction: column;
	}
	
	.item-line {
		width: 100%;
		height: 18rpx;
		background-color: #f5f5f5;
	}

	.list-item {
		background-color: #ffffff;
		width: 750rpx;
		height: 200rpx;
		display: flex;
		flex-direction: row;
		margin-top: 20rpx;
	}

	.list-item-left {
		width: 160rpx;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-end;
	}

	.list-item-middle {
		width: 490rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-start;
		height: 100%;
		margin-left: 32rpx;
	}

	.university-name {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
		color: #333333;
		height: 54rpx;
	}

	.university-label-box {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}

	.university-label {
		border: solid 1rpx #f1882b;
		font-size: 20rpx;
		color: #f1882b;
		padding-top: 4rpx;
		padding-bottom: 4rpx;
		padding-left: 10rpx;
		padding-right: 10rpx;
		margin-right: 8rpx;
		margin-top: 10rpx;
	}

	.list-item-left image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 60rpx;
	}
	
	.list-item-more {
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	
	.list-item-more image {
		width: 26rpx;
		height: 26rpx;
	}
</style>
